<?php
namespace app\common\logic;

use think\cache\driver\Redis;
use think\Controller;
use think\Db;
use think\Exception;

class Pocessing extends Controller
{
	/**
	 * @Purpose 异步读取所有数据
	 * @Time 2017-11-23
	 */
	public function asyncDataAll($lotType)
	{
		$retData = [];
		$djsh_time = time();
		$kjHao = null;
		$czid = $lotType;
		if(!$czid) exit;
		//获取当前一期时间
		try{
			$next = new Redis();
			$nextNo = $next->get($czid);
			if(!isset($nextNo[1]) || $nextNo[1]<$djsh_time){
				$ext = Db::name('kjjg_list')->where('czid',$czid)->order('qihao','DESC')->find();
				$next->set($czid, $ext['qihao'].",".$ext['jztime'].",0");
				$nextNo = $next->get($czid);
			}
		}catch (\Exception $e){
			$ext = Db::name('kjjg_list')->where('czid',$czid)->order('qihao','DESC')->find();
			$nextNo = $ext['qihao'].",".$ext['jztime'].",0";
		}
		$fp_time = Db::name('caipai_list')->where('id',$czid)->value('cp_fp_time');
		$nextNo = explode(',', $nextNo);
		$fp = $nextNo[1]-$djsh_time;
		//若无数据则表示还未开奖或暂未获取到数据
		$retData['czid']=$czid;
		/**@todo上一期号*/
		$before = Db::name('kjjg_list')->where('czid',$czid)->where("jztime","<",$nextNo[1])->order('qihao','DESC')->value('qihao');
		$retData['before'] = $before;
		$retData['next']['kjjg'] = $nextNo[2];
		$retData['next']['jztime'] = $nextNo[1]-$djsh_time;
		$retData['next']['fptime'] = $fp-$fp_time;
		$retData['next']['qihao0'] = $nextNo[0];

		if($czid==62 || $czid==63 || $czid==72){
			$retData['next']['qihao'] = substr($nextNo[0],4);
		}else{
			$retData['next']['qihao'] = $nextNo[0];
		}
		if($retData['next']['fptime']<0){
			$retData['next']['fptime']=0;
		}
		$retData['next']['awardTime'] = date('Y-m-d H:i:s',$nextNo[1]);
		$retData['next']['nexttime'] = $nextNo[1];

		return $retData;
	
	}
	public function asyncDataAll_new($lotType)
	{
		$retData = [];
		$djsh_time = time();
		$kjHao = null;
		$czid = $lotType;
		if(!$czid) exit;
		//获取当前一期时间
		try{
			$next = new Redis();
			$nextNo = $next->get($czid);
			if(!isset($nextNo[1]) || $nextNo[1]<$djsh_time){
				$ext = Db::name('kjjg_list')->where('czid',$czid)->order('qihao','DESC')->find();
				$next->set($czid, $ext['qihao'].",".$ext['jztime'].",0");
				$nextNo = $next->get($czid);
			}
		}catch (\Exception $e){
			$ext = Db::name('kjjg_list')->where('czid',$czid)->order('qihao','DESC')->find();
			$nextNo = $ext['qihao'].",".$ext['jztime'].",0";
		}
		$fp_time = Db::name('caipai_list')->where('id',$czid)->value('cp_fp_time');
		$nextNo = explode(',', $nextNo);
		$fp = $nextNo[1]-$djsh_time;
		//若无数据则表示还未开奖或暂未获取到数据
		$retData['czid']=$czid;
		/**@todo上一期号*/
		$before = Db::name('kjjg_list')->where('czid',$czid)->where("jztime","<",$nextNo[1])->order('qihao','DESC')->value('qihao');
		$retData['before'] = $before;
		$retData['next']['kjjg'] = $nextNo[2];
		$retData['next']['jztime'] = $nextNo[1]-$djsh_time;
		$retData['next']['fptime'] = $fp-$fp_time;
		$retData['next']['qihao0'] = $nextNo[0];
		$retData['next']['qihao'] = $nextNo[0];
		if($retData['next']['fptime']<0){
			$retData['next']['fptime']=0;
		}
		$retData['next']['awardTime'] = date('Y-m-d H:i:s',$nextNo[1]);
		$retData['next']['nexttime'] = $nextNo[1];
	
		return $retData;
	
	}
	
	public function asyncDataDetail($lotType,$nextIssue){

		$obj = new Redis();
		$kjjg = $obj->get("result_".$lotType);
		$data = array();
		if( $kjjg ){
			$jg = explode(',',$kjjg);
			if( $jg[0]==$nextIssue ){
				$data['czid'] = $lotType;
				$data['jztime'] = $jg[1];
				$data['qihao'] = $jg[0];
				$data['kjjg'] = kjjg_split( $jg[2], $lotType );
				$a = 0;
				foreach ( $data['kjjg'] as $ss){$a+=$ss;}
				if($lotType==36 || $lotType==44){
					$data['kjjg'][] = '=';
					$data['kjjg'][] = $a;
				}
			}
		}else{
			$kj = Db::name('kjjg_list')->where(['czid'=>$lotType,'qihao'=>$nextIssue])->field('kjjg,qihao')->find();
			if( $kj && !empty($kj['kjjg']) ){
				$data['czid'] = $lotType;
				$data['jztime'] = $kj['jztime'];
				$data['qihao'] = $kj['qihao'];
				$data['kjjg'] = kjjg_split( $kj['kjjg'], $lotType );
				$a = 0;
				foreach ( $data['kjjg'] as $ss){$a+=$ss;}
				if($lotType==36 || $lotType==44){
					$data['kjjg'][] = '=';
					$data['kjjg'][] = $a;
				}
			}
		}
		return $data;
	}
	function getMillisecond()
	{
		list ($s1, $s2) = explode(' ', microtime());
		return (float) sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
	}
	
}